/****************************************************
*	OSMF Edge Media Player version 1.2.2
* 
*	Author: Emanuele Manco
* 	E-Mail: hello@flashedge.net
*	Web: http://activeden.net/user/flashedge
*  
*****************************************************/

package net.flashedge
{
	// flash libraries
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.net.navigateToURL;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.Security;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.ui.Mouse;
	import flash.utils.Timer;
	
	//greensock framework
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.greensock.loading.SWFLoader;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.XMLLoader;
	
	// osmf classes
	import org.osmf.layout.ScaleMode;
	import org.osmf.media.MediaElement;
	
	// my custom classes
	import net.flashedge.display.*;
	import net.flashedge.utils.*;
	import net.flashedge.core.Playlist;
	import net.flashedge.events.Actions;
	
	public class Shell extends Sprite
	{
		// helper classes
		public var objects:Objects = new Objects();
		public var policies:Policies = new Policies();
		public var arranger:Arranger = new Arranger();
		public var actions:Actions = new Actions();
		public var pl:Playlist = new Playlist();
		public var des:Description = new Description();
		public var sp:SettingsPanel = new SettingsPanel();
		public var spectrum:Spectrum = new Spectrum();
		public var spectrumEnabled:Boolean = false;
		
		public var queue:LoaderMax;								// main queue loader
		public var setupPath:String = "xml/data/setup.xml";
		public var playlistPath:String = "xml/data/playlist.xml";
		public var mpPath:String = "swf/osmf.swf";
		
		public var gtrackXML:XML;
		public var gtrackSetupXML:String = "xml/data/gtrack.xml";
		public var gtrackPath:String;
		
		public var random:Array;
		
		public var playlist:Array;			// the whole playlist containing all entries
		public var content:Array;			// the current entry with ads, covers and all the gugas
		public var preroll:Array;			// preroll entries
		public var postroll:Array;			// postroll entries
		
		public var s:Number = 0;			// shuffled number
		public var n:Number = 0;			// entry number
		public var i:Number = 0;			// internal number
		
		public var scaleMode;
		
		public var youtubeEnabled:Boolean = false;
		
		public var playMode:String;
		
		public var started:Boolean = false;
		public var clicked:Boolean = false;
		public var mpLoaded:Boolean = false;
		public var hdClicked:Boolean = false;
		public var player:*;
		
		public var main:Sprite = new Sprite();
		
		public var path:String;
		
		public var w:Number = 640;
		public var h:Number = 360;
		public var oldW:Number;
		public var oldH:Number;
		public var oldX:Number;
		public var oldY:Number;
		public var fluidMode:Boolean = true;
		
		public var area:Sprite = new Sprite();
		public var gui:Sprite = new Sprite();
		public var controls:Sprite = new Sprite();
		public var seeker:Sprite = new Sprite();
		
		public var seekerPadding:Number = 280;
		
		public var controlsBg:Sprite = new Sprite();
		
		public var icons:Sprite = new Sprite();
		
		public var volumebar:Volume = new Volume();
		public var volumeLevel:Number = 1;
		public var volumeTooltip:Tooltip;
		
		public var seekbar:Seekbar = new Seekbar();
		public var setup:Setup = new Setup();
		public var timeUtil:TimeUtil = new TimeUtil();
		public var timeStamp:TimeStamp = new TimeStamp();
		public var seekerTooltip:Tooltip;
		
		public var playing:Boolean;
		
		public var currentTime;
		public var totalTime;
		
		public var timer:Timer;
		public var controlsActive:Boolean = false;
		
		public var pluginNum:Number = 0;
		
		public var currentQuality:Number = 0;
		public var maxQuality:Number = 0;
		
		public var resizeDelay:Timer = new Timer(500, 0);
		
		public function Shell()
		{
			root.loaderInfo.parameters.setup != null ? setupPath = root.loaderInfo.parameters.setup : trace ("using default setup path: " + setupPath);
			root.loaderInfo.parameters.playlist != null ? playlistPath = root.loaderInfo.parameters.playlist : trace ("using default playlist path: " + playlistPath);
			root.loaderInfo.parameters.player != null ? mpPath = root.loaderInfo.parameters.player : trace ("using default player path: " + mpPath);
			root.loaderInfo.parameters.gtrack != null ? gtrackSetupXML = root.loaderInfo.parameters.gtrack : trace ("using default gtrack xml setup: " + gtrackSetupXML);
				
			addEventListener(Event.ADDED_TO_STAGE, init);		
			addEventListener(Event.REMOVED_FROM_STAGE, clear);	
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				INITIALIZE FUNCTION
	// _____________________________________________________________________________________________________________	
	
		public function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.RESIZE, resize, false, 0, true);
			stage.addEventListener(FullScreenEvent.FULL_SCREEN, onFullScreenEvent);
			
			var local = (loaderInfo.url.substring(0, 4) != "http") ? true : false;
			local ? trace ("It's running locally, no policies to load.") : policies.load();
			
			// main loading queue
			queue = new LoaderMax( { maxConnections:1, onProgress: queueProgress, onComplete: queueComplete } );
			queue.append(new XMLLoader(setupPath, { name: "Setup", noCache: true, onComplete: initSetup} ));
			queue.append(new XMLLoader(playlistPath, { name: "Playlist", noCache: true, onComplete: initPlaylist } ));
			//queue.append(new XMLLoader(gtrackSetupXML, { name: "Gtrack", noCache: true, skipFailed:true, onComplete: initGtrackXML } )); // loader moved to the plugin part
			queue.append(new SWFLoader(mpPath, { name:"MediaPlayer", container:main, x:0, y:0, onProgress: mpProgress, onInit:mpInit } ));
			queue.load();
			
			addChild(main);
			//main.x = 100;
			//main.y = 100;
			oldW = w;
			oldH = h;
			oldX = main.x;
			oldY = main.y;
			
			Font.registerFont(secondaryFont);	// share secondary font at runtime
			
			
			objects.createMainMask(w, h);
			objects.createPlayerMask(w, h);
			objects.createSpectrumMask(w, h);
			objects.createHeaderBg(w, 36);
			objects.createBigButton(setup.playBtnWidth, setup.playBtnHeight, setup.playBtnRadius);
			
			gui.addChild(objects.header);
			
			gui.addChild(des);
			gui.addChild(pl);
			gui.addChild(sp);
			gui.addChild(objects.bigButton);
			
			objects.createShader(w, h);
			objects.createRing(w, h);
			
			main.addChild(objects.shader);
			gui.addChild(objects.ring);
			
			pl.alpha = sp.alpha = des.alpha = 0;
			pl.visible = sp.visible = des.visible = false;
			
			objects.header.visible = false;
			
			resize();
			
			actions.animate(objects.ring);
			main.addChild(objects.ring);
			
			TweenMax.allFrom([objects.shader, objects.ring], 1, { autoAlpha:0 } );

		}
		
	
		private function queueProgress(e:LoaderEvent):void {
			trace("Queue progress: " + e.target.progress);
			trace ("ring alpha: " + objects.ring.alpha);
			trace ("ring visible: " + objects.ring.visible);
			trace ("shader alpha: " + objects.shader.alpha);
			trace ("shader visible: " + objects.shader.visible);
			trace ("gui visible: " +gui.visible);
			trace ("gui alpha: " + gui.alpha);
			trace ("main visible:" +main.visible);
			trace ("main alpha: " + main.alpha);
			
			//arranger.place(objects.shader, stage.stageWidth, stage.stageHeight);
			//arranger.place(objects.ring, stage.stageWidth, stage.stageHeight);	
			} 
			
		private function queueComplete(e:LoaderEvent):void {
			trace("Queue loading complete.");
			} 
		
		private function mpProgress(e:LoaderEvent):void {
			trace("progress: " + e.target.progress);
			} 
		
		private function mpInit(e:LoaderEvent):void {
			player = e.target.content.rawContent;
			player.mask = objects.playerMask;
			player.init();
			
			// hitarea for youtube mode
			main.addChild(area);
			area.graphics.beginFill(0xFF0000, 0);
			area.graphics.drawRect(0, 0, w, h);
			area.graphics.endFill();
			
			if (setup.pluginEnabled == "false") {
				
				createPlayer();
				
				} else {
				// plugin traits	
				player.addEventListener("PluginLoaded", onPluginLoaded);
				player.addEventListener("PluginError", onPluginError);
				
				loadPlugin(pluginNum);
	
			}
		}
		
		private function loadPlugin(n:Number):void {
			var path:String = setup.plugins[n].path;
			var id:String = setup.plugins[n].id;
			
			switch (id) {
				case "gtrack": 
				// first load the gtrack setup xml file
				var gtrackLoader:XMLLoader = new XMLLoader(gtrackSetupXML, { name: "Gtrack", noCache: true, onComplete: initGtrackXML } );
				gtrackLoader.load();
				gtrackPath = path;
				// delay the plugin download
				// player.loadPlugin(id, path, gtrackXML);
				break;
				case "youtube": player.loadPlugin(id, path);
				break;
				case "tubemogul": player.loadPlugin(id, path, null, setup.data.plugins.module[n].@playerID, setup.data.plugins.module[n].@trackerID);
				break;
				case "mast": player.loadPlugin(id, path);
				break;
				}
		}
	
		private function initGtrackXML(e:LoaderEvent):void {
				gtrackXML = e.currentTarget.content;
				
				// now we can download the gtrack plugin
				player.loadPlugin("gtrack", gtrackPath , gtrackXML);
			}	
			
		private function initPlaylist(e:LoaderEvent):void {
				pl.setup = setup;
				var xmldata = e.currentTarget.content;
				
				random = new Array();
				playlist = new Array();
				
				preroll = new Array();
				content = new Array();
				postroll = new Array();
				
				for (var i = 0; i < xmldata.entry.length(); i++) {
				
					var entry:Array = new Array();
					
					// collects preroll items, such as logo videos or advertising
					var prerollList:XMLList = xmldata.entry[i].preroll.media;
					for each (var prerollElement:XML in prerollList) {
						trace ("Found element in preroll!");
						preroll.push( {
							path: prerollElement.@path,
							type: prerollElement.@type,
							url: prerollElement.@url,
							target: prerollElement.@target
							} )

						entry.push( {
							path: prerollElement.@path,
							type: prerollElement.@type,
							url: prerollElement.@url,
							target: prerollElement.@target
							} )
					}
					
					// creates the main entry of the video
					var contentList:XMLList = xmldata.entry[i].content.media;
					for each (var contentElement:XML in contentList) {
						trace ("Found element in content!");
						content.push( { 
							path: contentElement.@path,
							type: contentElement.@type,
							title: contentElement.title,
							description: contentElement.description,
							thumbnail: contentElement.image.@thumbnail,
							cover: contentElement.image.@full, 
							id: contentElement.meta.@id,
							name: contentElement.meta.@name,
							publisher: contentElement.meta.@publisher,
							category: contentElement.meta.@category,
							duration: contentElement.meta.@duration
							} )
							
						entry.push( { 
							path: contentElement.@path,
							type: contentElement.@type,
							title: contentElement.title,
							description: contentElement.description,
							thumbnail: contentElement.image.@thumbnail,
							cover: contentElement.image.@full, 
							id: contentElement.meta.@id,
							name: contentElement.meta.@name,
							publisher: contentElement.meta.@publisher,
							category: contentElement.meta.@category,
							duration: contentElement.meta.@duration
							} )
						
						}
						
					// all the postroll elements are stored here
					var postrollList:XMLList = xmldata.entry[i].postroll.media;
					for each (var postrollElement:XML in postrollList) {
						trace ("Found element in postroll!");
						postroll.push ( {
							path:postrollElement.@path,
							type:postrollElement.@type,
							url:postrollElement.@url,
							target:postrollElement.@target
							} )
						
						entry.push ( {
							path:postrollElement.@path,
							type:postrollElement.@type,
							url:postrollElement.@url,
							target:postrollElement.@target
							} )	
							
						}
					
					// finally we collect all arrays inside the main playlist
					playlist.push(entry);
					random.push(i);
					
					trace ("Playlist length: " + playlist.length);
					
					
				}
			
				shuffle(random);
				trace ("shuffled: " + random);
				
				pl.init(content);	// once we have all the data, start building the visual playlist!
				pl.addEventListener("PlaylistReady", plReady);
			}	
			
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				PLAYLIST FUNCTIONS
	// _____________________________________________________________________________________________________________	
	
			
		private function plReady(e:Event):void 
			{
				trace ("Playlist has been populated and is ready!");
				
				for (var i = 0; i < pl.entries.numChildren; i++) {
					var obj = pl.entries.getChildAt(i);
					obj.addEventListener(MouseEvent.CLICK, plClick);
					obj.addEventListener(MouseEvent.MOUSE_OVER, plOver);
					obj.addEventListener(MouseEvent.MOUSE_OUT, plOut);
				}
			}
			
			
			
		private function plClick(e:MouseEvent):void 
			{
				//i = 0;
				s = n = e.currentTarget.n;
				launch(playlist[e.currentTarget.n][0].path, playlist[e.currentTarget.n][0].type);
				
				trace ("Clicked video number: " + e.currentTarget.n);
				/*trace ("With title: " + e.currentTarget.title);
				trace ("And description: " +e.currentTarget.description);
				trace ("Duration of: " + e.currentTarget.duration);*/
			}
			
		private function plOut(e:MouseEvent):void 
			{
				var obj = e.currentTarget;
				var bg = obj.getChildByName("bg");
				var img = obj.getChildByName("img");
				TweenMax.to(bg, 0.5, { alpha: 0.8 } );
				TweenMax.to(img, 0.5, { colorMatrixFilter: { contrast:1, brightness:1, saturation:1 }, scaleX: 1, scaleY: 1, ease: Cubic.easeOut});
			}
			
		private function plOver(e:MouseEvent):void 
			{
				var obj = e.currentTarget;
				var bg = obj.getChildByName("bg");
				var img = obj.getChildByName("img");
				
				var tC = setup.playlistThumbContrast;
				var tB = setup.playlistThumbBrightness;
				var tS = setup.playlistThumbSaturation;
				var scale = setup.playlistThumbScale;
				
				TweenMax.to(bg, 0.5, { alpha: 1 } );
				TweenMax.to(img, 1, { colorMatrixFilter:{contrast:tC, brightness:tB, saturation: tS}, scaleX: scale, scaleY: scale, ease: Cubic.easeOut});
			}	
			
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				SHUFFLE FUNCTION
	// _____________________________________________________________________________________________________________	
			
		function shuffle(a:Array):void {
			var i:Number = a.length;
			if (i > 0) {
				while (--i) {
					var j:Number = Math.floor(Math.random() * (i + 1));
					var tmpi:Object = a[i];
					var tmpj:Object = a[j];
					a[i] = tmpj;
					a[j] = tmpi;
				}
			}
		}
			
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																						SETUP PARSING
	// _____________________________________________________________________________________________________________	
	
		private function initSetup(e:LoaderEvent):void {
				var xmldata = e.currentTarget.content;
				setup.parse(xmldata);					// copy the data to the helper class
				
				main.x = setup.playerX;
				main.y = setup.playerY;
				w = setup.playerWidth;
				h = setup.playerHeight;
				
				setup.fluidMode == "true" ? fluidMode = true : fluidMode = false;
				
				if (setup.infoButtonEnabled == "true") { des.init(); }
				
				des.setup = setup;
				
				initSettingsPanel();
				initMenu();
			}	
			
			private function initSettingsPanel():void
			{
				sp.setup = setup;
				sp.player = player;
				sp.init();
				sp.arrange(w, h);
				
				sp.settings.scale.addEventListener(MouseEvent.CLICK, toggleScale);
				
				spectrum.w = w;
				spectrum.h = h;
				spectrum.init();
				
				setup.headerEnabled == "true" ? objects.header.visible = true : objects.header.visible = false;
			}
			
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				SCALING FUNCTIONS
	// _____________________________________________________________________________________________________________	
			
			private function toggleScale(e:MouseEvent):void 
			{
				if (youtubeEnabled) {
					trace ("No scale mode for youtube.");
					sp.status.visible = false
				} else {	
					sp.status.visible = true;
					switch (player.mps.scaleMode) {
						case ScaleMode.NONE: 
						player.mps.scaleMode = ScaleMode.LETTERBOX;
						sp.statusField.text = setup.scaleLetterboxLabel;
						sp.arrange(w, h);
						break;
						case ScaleMode.LETTERBOX: 
						player.mps.scaleMode = ScaleMode.STRETCH;
						sp.statusField.text = setup.scaleStretchLabel;
						sp.arrange(w, h);
						break;
						case ScaleMode.STRETCH: 
						player.mps.scaleMode = ScaleMode.ZOOM;
						sp.statusField.text = setup.scaleZoomLabel;
						sp.arrange(w, h);
						break;
						case ScaleMode.ZOOM: 
						player.mps.scaleMode = ScaleMode.NONE;
						sp.statusField.text = setup.scaleNoneLabel;
						sp.arrange(w, h);
						break;
					}
				}
				
				trace ("actual scale mode is now: " + player.mps.scaleMode);
				sp.refreshBg();
				TweenMax.to (sp.status, 0.5, { alpha:1 } );
			}
				
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				PLUGIN EVENTS
	// _____________________________________________________________________________________________________________	
	
		private function onPluginError(e:Event):void 
		{
			trace ("Error loading Plugin");
		}
		
		private function onPluginLoaded(e:Event):void 
		{
			trace ("Plugin loaded successfully!");
			
			pluginNum++;
			
			if (pluginNum == setup.plugins.length) {
				createPlayer();
			} else {
				var remaining:String = ("Still " + (setup.plugins.length - pluginNum) + " plugins to load...");
				loadPlugin(pluginNum);
			}
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				CREATE THE PLAYER
	// _____________________________________________________________________________________________________________	
		
		private function createPlayer():void
		{	
			player.path = content[0].path;
				
			mpLoaded = true;
			
			player.mps.mediaPlayer.autoDynamicStreamSwitch = false;
			player.mps.mediaPlayer.volume = setup.volumeLevel;
			//player.mps.mediaPlayer.bufferTime = setup.bufferTime;
			
			switch (setup.scaleMode) {
				case "none": player.mps.scaleMode = scaleMode = ScaleMode.NONE;
				break;
				case "letterbox": player.mps.scaleMode = scaleMode = ScaleMode.LETTERBOX;
				break;
				case "stretch": player.mps.scaleMode = scaleMode = ScaleMode.STRETCH;
				break;
				case "zoom": player.mps.scaleMode = scaleMode = ScaleMode.ZOOM;
				break;
				}
	
			player.mps.width = stage.stageWidth;
			player.mps.height = stage.stageHeight;
			player.mps.addEventListener(Event.RESIZE, resize);
			
			player.mps.mediaContainer.backgroundAlpha = setup.playerBackgroundAlpha;
			player.mps.mediaContainer.backgroundColor = setup.playerBackgroundColor;
			
			arrangeToolbar();
			createGraphics();
			
			timer = new Timer(setup.hideDelay*1000, 0);
			timer.addEventListener(TimerEvent.TIMER, timerOver);
			timer.start();
			
			main.addEventListener(MouseEvent.ROLL_OVER, showControls);
			//main.addEventListener(MouseEvent.ROLL_OUT, hideControls);
			main.addEventListener(MouseEvent.MOUSE_MOVE, handleControls);
			main.addEventListener(MouseEvent.CLICK, handleControls);
			
			//main.addEventListener(MouseEvent.ROLL_OUT, hideControls);
			stage.addEventListener(Event.MOUSE_LEAVE, mouseLeave);
			
			player.addEventListener("TimeChange", onCurrentTimeChange);
			player.addEventListener("BufferChange", onBufferChange);
			player.addEventListener("PlayChange", onPlayChange);
			player.addEventListener("LoadChange", onLoadChange);
			player.addEventListener("StateChange", onStateChange);
			player.addEventListener("TimeCompleted", onTimeCompleted);
			
			objects.bigButton.addEventListener(MouseEvent.ROLL_OVER, bigButtonOver);
			objects.bigButton.addEventListener(MouseEvent.ROLL_OUT, bigButtonOut);
			
			objects.refreshBigButton(setup.playBtnWidth, setup.playBtnHeight, setup.playBtnRadius);
			
			setup.autoPlay == "true" ? launch(playlist[n][i].path, playlist[n][i].type) : launch(content[n].cover, "image");
			
			resize();
		}
		
		private function bigButtonOut(e:MouseEvent):void 
		{
			TweenMax.to(objects.bigButtonBg, 0.5, { alpha:0.7 } );
			/*TweenMax.to(objects.bigPlayBtn, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0, blurX:0, blurY:0 } } );
			TweenMax.to(objects.replayBtn, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0, blurX:0, blurY:0 } } );*/
		}
		
		private function bigButtonOver(e:MouseEvent):void 
		{
			TweenMax.to(objects.bigButtonBg, 0.5, { alpha:1 } );
			/*TweenMax.to(objects.bigPlayBtn, 0.5, { glowFilter: { color:0xFFFFFF, alpha:1, blurX:8, blurY:8 } } );
			TweenMax.to(objects.replayBtn, 0.5, { glowFilter: { color:0xFFFFFF, alpha:1, blurX:8, blurY:8 } } );*/
		}
		
		private function mouseLeave(e:Event):void 
		{
			hideControls();
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				TOOLBAR FUNCTIONS
	// _____________________________________________________________________________________________________________	
		
		private function arrangeToolbar():void
		{	
			var activeNum = 0;
			var spacing = 24;
			var startPoint = 0;
		
			for (var i = 0; i < objects.toolbar.numChildren-1; i++ ) {
				
					if (setup.toolbarBtns[i].enabled == "true") {
					
					var obj = objects.toolbar.getChildAt(i);
					var tp = (obj + "Tooltip");
					var label = setup.toolbarBtns[i].label;
					
					tp = new Tooltip(8, label, setup.tooltipBgColor, setup.tooltipTextColor, 1, true, "down", null, true);
					
					objects.toolbarTooltips.addChild(tp)
					
					tp.name = setup.toolbarBtns[i].name;
					
					tp.x = Math.floor(obj.x - tp.width * .5);
					tp.y = 26;
					tp.alpha = 0;
					
					obj.addEventListener(MouseEvent.MOUSE_OVER, toolBtnOver);
					obj.addEventListener(MouseEvent.MOUSE_OUT, toolBtnOut);
					obj.addEventListener(MouseEvent.CLICK, toolBtnClick);
					//obj.buttonMode = true;
					
					activeNum++;
				} else {
					objects.toolbar.getChildAt(i).visible = false;
				}
			}
			
			// this part arranges the whole toolbar and checks disabled buttons
			
			startPoint = -((activeNum-1) * spacing)-10;
			var num = 0;
			
			for (var j = 0; j < objects.toolbar.numChildren - 1; j++ ) {
				var toolBtn = objects.toolbar.getChildAt(j);
				
				
					trace ("Is the toolbutton visible?" + toolBtn.visible);
					
				if (toolBtn.visible) {
					toolBtn.x = startPoint + (spacing * num);
					
					for (var k = 0; k < objects.toolbarTooltips.numChildren -1; k++) {
						var toolTp = objects.toolbarTooltips.getChildAt(k);
						
						if (toolTp.name == toolBtn.name) {
							toolTp.x = toolBtn.x - (toolTp.width*.5)
							}
						}
					
					num++;
					
					} 
			}
		}
		
		private function toolBtnOver(e:MouseEvent):void 
		{
			var tp = (e.currentTarget.name);
			var tt = objects.toolbarTooltips;
			var obj = tt.getChildByName(tp);
			
			obj.x = Math.floor(obj.x);
			trace ("x:"+obj.x +" y: "+ obj.y);
			
			TweenMax.to(obj, 0.3, { autoAlpha: 1, y: 30, ease: Cubic.easeOut } );
			TweenMax.to(e.currentTarget, 0.5, { alpha: 1, ease: Cubic.easeOut } );
		}
		
		private function toolBtnOut(e:MouseEvent):void 
		{	var tp = (e.currentTarget.name);
			var tt = objects.toolbarTooltips;
			var obj = tt.getChildByName(tp);
			
			TweenMax.to(obj, 0.3, { autoAlpha: 0, y: 26, ease: Cubic.easeOut } );
			TweenMax.to(e.currentTarget, 0.5, { alpha: 0.6, ease: Cubic.easeOut } );
		}
		
		private function toolBtnClick(e:MouseEvent):void 
		{
			trace ("Clicked on: " + e.currentTarget.name);
			var pageURL:String = ExternalInterface.call('window.location.href.toString');
			
			switch (e.currentTarget.name) {
			case "infoButton": togglePanel(des); hidePanels(des);
			break;
			case "playlistButton": togglePanel(pl); hidePanels(pl);
			break;
			case "settingsButton": togglePanel(sp); hidePanels(sp);
			break;
			case "tweetButton":
			navigateToURL(new URLRequest("http://twitter.com/?status="+pageURL), "_blank");
			break;
			case "likeButton":
			navigateToURL(new URLRequest("http://www.facebook.com/sharer.php?u="+pageURL), "_blank");
			break;
			}
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				PANELS FUNCTIONS
	// _____________________________________________________________________________________________________________	
		
		private function togglePanel(obj):void
		{	
			if (obj.enabled) {
				TweenMax.to(obj, 0.5, { autoAlpha:1 } );
				obj.enabled = false;
				TweenMax.to(objects.bigButton, 0.5, { autoAlpha:0 } );
			} else {
				TweenMax.to(obj, 0.5, { autoAlpha:0 } );
				obj.enabled = true;
				if (obj == sp) { TweenMax.to (sp.status, 0.5, { alpha:0 } ); };
			}
		}
		
		private function hidePanels(obj = null):void {
			switch (obj) {
				case des:
				pl.enabled ? trace("Playlist already hidden."): togglePanel(pl);
				sp.enabled ? trace("Settings already hidden."): togglePanel(sp);	
				break;
				
				case pl:
				des.enabled ? trace("Description already hidden."): togglePanel(des);
				sp.enabled ? trace("Settings already hidden."): togglePanel(sp);	
				break;
				
				case sp:
				des.enabled ? trace("Description already hidden."): togglePanel(des);
				pl.enabled ? trace("Playlist already hidden."): togglePanel(pl);
				break;
				
				case null:
				if (playMode == "image" && setup.playlistAtEnding == "true") {
					trace ("there is no need to hide the playlist");
					} else {
					pl.enabled ? trace("Playlist already hidden."): togglePanel(pl);
					}
				
				des.enabled ? trace("Description already hidden."): togglePanel(des);
				sp.enabled ? trace("Settings already hidden."): togglePanel(sp);	
				break;
			}
		}
	
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				MEDIA LAUNCHING FUNCTION
	// _____________________________________________________________________________________________________________		
		
		public function launch(path:String, type:String = "video"):void {
			
			youtubeEnabled = false;
			var p:int;
			
			sp.shuffleEnabled ? p = s : p = n;
		
			playMode = type;
			objects.titleField.htmlText = content[p].title;
			
			area.visible = false;
			
			switch (type) {
				case "content":
				sp.showScale();
				sp.arrange(w, h);
				
				spectrum.enabled = false;
				TweenMax.to(objects.bigButton, 0.5, { autoAlpha:0 } );
				//setup.seekbarProgressColor = 0xFFFFFF;
				player.launch(path, type, playlist[p][i].id, playlist[p][i].name, playlist[p][i].publisher, playlist[p][i].category);
				
				trace ("Meta tags found! ");
				trace ("ID: " + playlist[p][i].id + " TITLE: " + playlist[p][i].name + " PUBLISHER: " + playlist[p][i].publisher + " CATEGORY: " + playlist[p][i].category);
				seeker.alpha = 1;
				break;
				
				case "advert":
				spectrum.enabled = false;
				//setup.seekbarProgressColor = 0xB2B2B2;
				player.launch(path, type);
				seeker.alpha = .5;
				break;
				
				case "vast":
				spectrum.enabled = false;
				//setup.seekbarProgressColor = 0xB2B2B2;
				player.launch(path, type);
				seeker.alpha = .5;
				break;
				
				case "video":
				sp.showScale();
				sp.arrange(w, h);
				
				spectrum.enabled = false;
				//setup.seekbarProgressColor = 0xFFFFFF;
				player.launch(path, type);
				seeker.alpha = 1;
				break;
				
				case "audio":
				//setup.seekbarProgressColor = 0xFFFFFF;
				sp.hideScale();
				sp.arrange(w, h);
				
				spectrum.enabled = true;
				spectrum.activate();
				player.launch(path, type);
				seeker.alpha = 1;
				break;
				
				case "image":
				i = 0;
				spectrum.enabled = false;
				player.launch(path, type);
				player.mps.alpha = 0;
				
				if (setup.playlistAtEnding == "true" && started) {	
					togglePanel(pl);	
					hidePanels(pl);		
					hideControls();
				} else {
					objects.bigButton.addEventListener(MouseEvent.CLICK, playContent);
					player.mps.addEventListener(MouseEvent.CLICK, playContent);
					hideControls();
				}
						
				break;
			}
			
			des.clearDescription();
			trace ("changing description for p number: " + p);
			des.create( content, p );	// change description
			
			if (player.checkYoutube(player.element) || playMode == "youtube") {
				sp.hideScale();
				sp.arrange(w, h);
				
				player.mps.scaleMode = ScaleMode.STRETCH; 
				area.visible = true;
				
			} else {
				
				player.mps.scaleMode = scaleMode;
				
				}
			
			started = true;
		}
		
		private function playContent(e:MouseEvent):void 
		{
			objects.bigButton.removeEventListener(MouseEvent.CLICK, playContent);
			player.mps.removeEventListener(MouseEvent.CLICK, playContent);
			TweenMax.to(objects.bigButton, 0.5, { autoAlpha:0 } );
			launch(playlist[n][i].path, playlist[n][i].type);
			
			TweenMax.to(objects.bigPlayBtn, 0.5, { autoAlpha:1, delay:0.5 } );
			TweenMax.to(objects.replayBtn, 0.5, { autoAlpha:0, delay:0.5 } );
			
			clicked = true;
		}
		
		private function refreshClicktag(type:String):void
		{
			if (type == "advert") {
					player.addEventListener(MouseEvent.CLICK, advertClick);
					player.buttonMode = true;
				} else {
					if (player.hasEventListener(MouseEvent.CLICK)) { 
						player.removeEventListener(MouseEvent.CLICK, advertClick) 
						player.buttonMode = false;
						};
				}
		}
		
		private function advertClick(e:MouseEvent):void 
		{
			if (objects.bigButton.alpha == 0) {
				var clickURL:String = playlist[n][i].url;	
				var clickTarget:String = playlist[n][i].target;
				
				trace ("Opening external URL: "+clickURL);
				navigateToURL(new URLRequest(clickURL), clickTarget);	
			}
		}
	
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				YOUTUBE HACK FOR RESIZING
	// _____________________________________________________________________________________________________________	

		
		private function resizeYoutube(e:Event = null):void
		{
			if (youtubeEnabled && player.checkYoutube(player.mps.media)) {
				var element = player.element;
				element.youTubePlayer.scaleX = element.youTubePlayer.scaleY = 1;
				element.youTubePlayer.x = element.youTubePlayer.y = 0;
				element.youTubePlayer.setSize(w, h);
				player.mps.mediaContainer.width = w;
				player.mps.mediaContainer.height = h;
			}
		}
	
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				QUALITY SWITCHING
	// _____________________________________________________________________________________________________________	
		
	
		private function switchQuality(e:MouseEvent):void 
		{
			trace ("Switching quality...");
			var i = player.mps.mediaPlayer.currentDynamicStreamIndex;
			var m = player.mps.mediaPlayer.maxAllowedDynamicStreamIndex;
			
			if (m > 2 ) {	// check if hd is available
				trace ("HD is available!");
				i < 3 ? i = m : i = 2;
				i > 2 ? objects.hdBtn.alpha = 1 : objects.hdBtn.alpha = 0.5;
				trace ("Switched to quality: " + i);
				player.mps.mediaPlayer.switchDynamicStreamIndex(i);
				
				hdClicked = true;
			}
		}
		
		private function checkQuality():void
		{
			currentQuality = player.mps.mediaPlayer.currentDynamicStreamIndex;
			maxQuality = player.mps.mediaPlayer.maxAllowedDynamicStreamIndex;
				
			trace ("Current Quality is: " + currentQuality);
			trace ("Maximum Quality is: " + maxQuality);
			
			if (maxQuality > 2) {
				// show hd icon
				objects.hdBtn.visible = true;
				
				if (currentQuality > 2) {
					objects.hdBtn.alpha = 1;
					} else {
					objects.hdBtn.alpha = 0.5;	
					}
				} else {
				// hide hd icon	
				objects.hdBtn.visible = false;
				}
			
		}
		
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				SHOW & HIDE GUI
	// _____________________________________________________________________________________________________________
		
		private function timerOver(e:TimerEvent):void 
		{
			hideControls();
		}
		
		private function hideControls(e:MouseEvent = null):void { 
			TweenMax.to(controls, 0.5, { y:h+8, ease:Cubic.easeOut } );  controlsActive = false; /*Mouse.hide();*/ 
			TweenMax.to(objects.header, 0.5, { y: -objects.header.height, ease:Cubic.easeOut } );
	
			hidePanels();
			
			stage.displayState == "fullScreen" ? Mouse.hide() : Mouse.show();
			
			}
		private function showControls(e:MouseEvent = null):void { 
			if (player.loadState == "ready" && playMode != "image") {  TweenMax.to(controls, 0.5, { y:h - setup.controlsPadding - controlsBg.height + setup.controlsOffset, ease:Cubic.easeOut } ); controlsActive = true; Mouse.show(); 
			if (playMode != "advert" && playMode != "vast" && playMode != "image") { TweenMax.to(objects.header, 0.5, { y:0, ease:Cubic.easeOut } ) }; 
			
			setup.headerEnabled == "true" ? objects.header.visible = true : objects.header.visible = false;
			//hidePanels();
			Mouse.show();
			} 
			}
		
		
		
		private function handleControls(e:MouseEvent = null):void
		{
			if (main.hitTestPoint(mouseX, mouseY)) { showControls(); } else { hideControls(); };
			timer.reset();
			timer.start();
		}
			
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				GRAPHICS GENERATION
	// _____________________________________________________________________________________________________________
		
		private function createGraphics():void
		{
			objects.createTitle(content[0].title, setup.titleColor, setup.titleSize, setup.titleX, setup.titleY );
			
			main.removeChild(objects.ring);
			
			// controls main container
			main.addChild(objects.mainMask);
			main.addChild(objects.playerMask);
			
			main.addChild(objects.spectrumMask);
			main.addChild(spectrum);	// sound spectrum
			spectrum.mask = objects.spectrumMask;
			
			main.addChild(gui);
			gui.addChild(controls);
			gui.addChild(objects.ring);
			
			controls.y = h;
			
			gui.mask = objects.mainMask;
			objects.header.visible = true;
			
			// controls background sprite
			drawControlsBg();

			controls.addChild(controlsBg);
			
			// seeker main container
			controls.addChild(seeker);
			seeker.y = 14;
			
			// seekbar creation
			seeker.addChild(seekbar);
			
			var seekerWidth = getSeekerWidth(w);
	
			seekbar.create(seekerWidth, setup.seekbarHeight, setup.seekbarRadius, setup.seekbarTrackColor, setup.seekbarProgressColor, setup.seekbarCacheColor, setup.seekbarPadding);
			
			// create tooltip for seeker
			seekerTooltip = new Tooltip(8, "00:00", setup.tooltipBgColor, setup.tooltipTextColor, 1, true, "up", null, true);
			seeker.addChild(seekerTooltip);
			seekerTooltip.alpha = 0;
			seekerTooltip.item.x = 2;
			seekerTooltip.y = -30;
			seekerTooltip.visible = false;	
			
			// create tooltip for volume
			volumeTooltip = new Tooltip(8, "100%", setup.tooltipBgColor, setup.tooltipTextColor, 1, true, "up", null, true);
			controls.addChild(volumeTooltip);
			volumeTooltip.y = -16;
			volumeTooltip.y = -16;
			volumeTooltip.item.x = 2;
			volumeTooltip.visible = false;
			volumeTooltip.alpha = 0;
			
			
			// timestamps
			currentTime = new TimeStamp();
			controls.addChild(currentTime);
			
			totalTime = new TimeStamp();
			controls.addChild(totalTime);
			
			// control icons
			controls.addChild(icons);
			
			// play - pause icons
			icons.addChild(objects.playBtn);
			icons.addChild(objects.pauseBtn);
			
			objects.playBtn.buttonMode = true;
			objects.pauseBtn.buttonMode = true;
			
			objects.playBtn.y = objects.pauseBtn.y = 60;
			objects.playBtn.alpha = objects.pauseBtn.alpha = 0;
			
			// forward - back icons
			/*icons.addChild(objects.forwardBtn);
			icons.addChild(objects.backBtn);
			objects.forwardBtn.y = objects.backBtn.y = 60;
			objects.forwardBtn.buttonMode = true;
			objects.backBtn.buttonMode = true;*/
			
			// volume bar
			icons.addChild(objects.volumeBtn);
			objects.volumeBtn.buttonMode = true;
			objects.volumeBtn.y = 9;
			drawVolumeStatus(setup.volumeLevel);
			
			controls.addChild(volumebar);
			objects.volumeBtn.addEventListener(MouseEvent.MOUSE_OUT, hideVolumeTooltip);
			volumebar.addEventListener(MouseEvent.MOUSE_OUT, hideVolumeTooltip);
			volumebar.create(100, setup.volumebarHeight, setup.volumebarRadius, setup.volumebarTrackColor, setup.volumebarLevelColor, setup.volumebarPadding);
			// set the initial level
			volumebar.updatelevel(setup.volumeLevel, setup.volumebarHeight, setup.volumebarRadius, setup.volumebarLevelColor, setup.volumebarPadding);
			volumebar.y = 14;
			
			icons.addChild(objects.fsBtn);
			icons.addChild(objects.hdBtn);
			
			/*icons.addChild(objects.shareBtn);
			icons.addChild(objects.shuffleBtn);
			icons.addChild(objects.playmodeBtn);*/
			
			var controlsFilters = new GlowFilter(0x000000, 0.5, 6, 6, 2);
			//controls.filters = [controlsFilters];
			
			
			var iconFilters = new DropShadowFilter(2, 45, 0x000000, 0.4, 2, 2);
			icons.filters = [iconFilters];
			
			addEventListeners();	// all event listeners
			
			resize();
		}
		
		private function drawControlsBg():void
		{
			controlsBg.graphics.clear();
			controlsBg.graphics.lineStyle(1, 0xE2E2E2, 0.2, true);
			controlsBg.graphics.beginFill(0x000000, 0.7);
			controlsBg.graphics.drawRoundRect(setup.controlsPadding, 0, w-(setup.controlsPadding*2), 37, setup.controlsRadius, setup.controlsRadius);
			//controlsBg.graphics.drawRect(0, 0, w, 200);
			controlsBg.graphics.endFill();
		}
	
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				PLAY/PAUSE BEHAVIOUR
	// _____________________________________________________________________________________________________________
		
		private function pauseClick(e:MouseEvent):void 
		{
			player.mps.mediaPlayer.pause();
			
			objects.bigPlayBtn.visible = true;
			objects.replayBtn.visible = false;
		}
		
		private function playClick(e:MouseEvent):void 
		{
			player.mps.mediaPlayer.play();
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																					MOUSE OVERS AND OUTS
	// _____________________________________________________________________________________________________________
	
		private function iconOut(e:MouseEvent):void 
		{
			var obj:String = e.currentTarget.name;
			TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0, blurX:0, blurY:0 } } );
		}
		
		private function iconOver(e:MouseEvent):void 
		{
			TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:1, blurX:6, blurY:6 } } );
		}
	
	
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																					RESIZE FUNCTIONS
	// _____________________________________________________________________________________________________________
		
		private function resize(e:Event=null):void 
		{	
			if (fluidMode) {
					w = stage.stageWidth;
					h = stage.stageHeight;
				} 
				
			if (w < 400 || h < 250) {
				controls.visible = false;
				objects.header.visible = false;
			} else {
				controls.visible = true;
				objects.header.visible = true;
			}
				
			arranger.place(objects.shader, w, h);
			arranger.place(objects.ring, w, h);	
			
			
			if (mpLoaded) {
				
				trace("Stage resized: " + stage.stageWidth + ", " + stage.stageHeight);
				des.arrange(w, h);
				pl.arrange(w, h);
				sp.arrange(w, h);
				
				arrangePlayer();
				
				if (youtubeEnabled) { 
					//resizeYoutube();
					
					resizeDelay.stop();
					resizeDelay.reset();
					resizeDelay.addEventListener(TimerEvent.TIMER, delayedResize);
					resizeDelay.start();
					
					player.addEventListener(Event.ENTER_FRAME, forceResize);
				};
				
				spectrum.arrange(w, h);
				
				setup.headerEnabled == "true" ? objects.header.visible = true : objects.header.visible = false;
			}
		}
		
		private function arrangePlayer():void
		{
			player.mps.width = w;
			player.mps.height = h;
			
			objects.mainMask.width = objects.spectrumMask.width = objects.playerMask.width = w;
			objects.mainMask.height = objects.spectrumMask.height = objects.playerMask.height = h;
			
			objects.headerBg.width = w+10;
			objects.toolbar.x = Math.floor(w - 10);
			objects.bigButton.x = Math.floor(w * .5);
			objects.bigButton.y = Math.floor(h * .5);
			
			controlsActive ? controls.y = h - setup.controlsPadding - controlsBg.height + setup.controlsOffset: controls.y = h;
			drawControlsBg();
			//controlsBg.width = w;
			
			updateSeeker(w);
			
			totalTime.y = currentTime.y = 9;
			currentTime.x = Math.floor(seeker.x - 15 - currentTime.width);
			totalTime.x = Math.floor( seeker.x + seekbar.track.width + 15);
			
			arranger.placeButtons(objects, w, h, setup.controlsPadding);
			
			updateVolume(w);
			
			arrangeArea();
		}
		
		private function forceResize(e:Event):void 
		{
			resizeYoutube();	// overwrites the silly resize function of the youtube plugin
		}
		
		private function delayedResize(e:TimerEvent):void 
		{
			player.removeEventListener(Event.ENTER_FRAME, forceResize);
			resizeDelay.stop();
			resizeDelay.reset();
			resizeYoutube();
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																							LISTENERS
	// _____________________________________________________________________________________________________________
		
		private function addEventListeners():void
		{
			
			seekbar.hit.addEventListener(MouseEvent.CLICK, trackClick);
			seekbar.hit.addEventListener(MouseEvent.MOUSE_MOVE, refreshTooltip);
			seekbar.hit.addEventListener(MouseEvent.MOUSE_OVER, seekerOver);
			seekbar.hit.addEventListener(MouseEvent.MOUSE_OUT, seekerOut);
			seekbar.hit.addEventListener(MouseEvent.MOUSE_DOWN, catchScrubber);
			
			volumebar.hit.addEventListener(MouseEvent.CLICK, volumebarClick);
			volumebar.hit.addEventListener(MouseEvent.MOUSE_DOWN, scrubVolume);
			
			spectrum.addEventListener(MouseEvent.CLICK, togglePlay);
			objects.playBtn.addEventListener(MouseEvent.CLICK, playClick);
			objects.pauseBtn.addEventListener(MouseEvent.CLICK, pauseClick);
			objects.volumeBtn.addEventListener(MouseEvent.CLICK, volumeClick);
			
			area.addEventListener(MouseEvent.CLICK, onClick);	
			
			for (var i = 0; i < icons.numChildren; i++) {
				var obj = icons.getChildAt(i);
				obj.addEventListener(MouseEvent.MOUSE_OVER, iconOver);
				obj.addEventListener(MouseEvent.MOUSE_OUT, iconOut);
				obj.buttonMode = true;
				}
				
			objects.fsBtn.addEventListener(MouseEvent.CLICK, toggleFullscreen);	
			objects.hdBtn.addEventListener(MouseEvent.CLICK, switchQuality);

		}
		
		private function togglePlay(e:MouseEvent):void 
		{
			if (player.mps.mediaPlayer.canPause) {
			player.mps.mediaPlayer.playing ? player.mps.mediaPlayer.pause() : player.mps.mediaPlayer.play();
			}
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																						SEEK ACTIONS
	// _____________________________________________________________________________________________________________
		
	
		private function updateSeeker(w:Number):void
		{
			var progressAmount:Number;
			var cacheAmount:Number;
			
			var seekerWidth = getSeekerWidth(w);
			
			if (player.mps.mediaPlayer.canSeek) {
				progressAmount = player.mps.mediaPlayer.currentTime / player.mps.mediaPlayer.duration;
				cacheAmount = player.mps.mediaPlayer.bytesLoaded / player.mps.mediaPlayer.bytesTotal;
			} else {
				progressAmount = cacheAmount = 0;
				}
				
			cacheAmount = (player.mps.mediaPlayer.bytesTotal == 0)? 1: player.mps.mediaPlayer.bytesLoaded / player.mps.mediaPlayer.bytesTotal;
			
			seekbar.resize(seekerWidth, setup.seekbarHeight, setup.seekbarRadius,setup.seekbarTrackColor, setup.seekbarProgressColor, setup.seekbarCacheColor, setup.seekbarPadding, cacheAmount, progressAmount);
			seeker.x = Math.floor(98 + setup.controlsPadding);
		}
		
		private function getSeekerWidth(w:Number):Number
		{
			var seekerOffset:Number = 0;
			
			maxQuality > 2 ? seekerOffset = 34 : seekerOffset = 0;
			
			var seekerWidth:Number = Math.floor(w - seekerPadding - (setup.controlsPadding * 2) - seekerOffset);
			
			return seekerWidth;
		}
		
		private function trackClick(event:MouseEvent):void
		{
			if (player.mps.mediaPlayer.canSeek && playMode != "advert" && playMode != "vast") {
			player.mps.mediaPlayer.seek((seekbar.track.mouseX / seekbar.track.width) * player.mps.mediaPlayer.duration);
			
			var progressAmount:Number = player.mps.mediaPlayer.currentTime / player.mps.mediaPlayer.duration;
			seekbar.updateProgress(progressAmount, setup.seekbarHeight, setup.seekbarRadius, setup.seekbarProgressColor, setup.seekbarPadding);
			}
		}
		
		private function refreshTooltip(e:MouseEvent):void 
		{
			var seconds = (seekbar.track.mouseX / seekbar.track.width) * player.mps.mediaPlayer.duration;
			var currentTime = timeUtil.formatTime(seconds);
			var totalTime = timeUtil.formatTime(player.mps.mediaPlayer.duration);
			//tooltip.item.htmlText = String(currentTime + "/" + totalTime);
			seekerTooltip.item.htmlText = String(currentTime);
			seekerTooltip.item.x = Math.floor((seekerTooltip.width - seekerTooltip.item.width) * .5);
			seekerTooltip.x = Math.floor(seekbar.track.mouseX - seekerTooltip.width*.5);
		}
		
		
		private function catchScrubber(e:MouseEvent):void 
		{
			if (playMode != "advert" && playMode != "vast") {
				player.mps.mediaPlayer.playing ? playing = true : playing = false;
				player.mps.mediaPlayer.pause();
				TweenMax.to(objects.bigButton, 0.5, { autoAlpha:0, overwrite:true } );
				seeker.addEventListener(MouseEvent.MOUSE_MOVE, trackClick, false, 0, true);
				stage.addEventListener(MouseEvent.MOUSE_UP, resumeScrubbing, false, 0, true);
				stage.addEventListener(MouseEvent.MOUSE_MOVE, trackClick, false, 0, true);
			}
		}
		
		private function resumeScrubbing(e:MouseEvent):void 
		{
			playing ? player.mps.mediaPlayer.play() : player.mps.mediaPlayer.pause();
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, trackClick);
			stage.removeEventListener(MouseEvent.MOUSE_UP, resumeScrubbing);
			seeker.removeEventListener(MouseEvent.MOUSE_MOVE, trackClick);
			seeker.removeEventListener(MouseEvent.MOUSE_UP, resumeScrubbing);
		}
		
		private function seekerOut(e:MouseEvent):void 
		{
			TweenMax.to(seekerTooltip, 0.5, { autoAlpha:0 } );
			//TweenMax.to(seekbar.progress, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0, blurX:0, blurY:0 } } );
		}
		
		private function seekerOver(e:MouseEvent):void 
		{
			playMode != "vast" && playMode != "advert" ? TweenMax.to(seekerTooltip, 0.5, { autoAlpha:1 } ) : seekerTooltip.visible = false;
			//TweenMax.to(seekbar.progress, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0.5, blurX:2, blurY:2, strength:4 } } );
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																						TIME MAPPING
	// _____________________________________________________________________________________________________________
		
		private function onLoadChange(event:Event):void {
			var cacheAmount:Number = player.mps.mediaPlayer.bytesLoaded / player.mps.mediaPlayer.bytesTotal;
			trace ("Bytes loaded: " + player.mps.mediaPlayer.bytesLoaded + "of" + player.mps.mediaPlayer.bytesTotal);
			seekbar.updateCache(cacheAmount, setup.seekbarHeight, setup.seekbarRadius, setup.seekbarCacheColor, setup.seekbarPadding);
			
			if (player.loadState == "ready") { 
				 TweenMax.allTo([objects.shader, objects.ring], 0.5, { autoAlpha:0 } )
				 //showControls();
				 
				checkQuality();
				
				//resize();
				
				if (playMode == "image") {
					TweenMax.to(player.mps, 0.5, { alpha:1 } );
					
					if (clicked) {
						objects.bigPlayBtn.visible = false;
						objects.replayBtn.visible = true;
					}	
					if (setup.playlistAtEnding != "true" || clicked == false) {
						TweenMax.to(objects.bigButton, 0.5, { autoAlpha:1, delay:0.2 } );
					} else {
						TweenMax.to(objects.bigButton, 0.5, { autoAlpha:0 } );
						}
				}
				
				refreshClicktag(playMode);
			}
			
			checkQuality();
			
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				HIT AREA FOR YOUTUBE
	// _____________________________________________________________________________________________________________
	
	private function arrangeArea():void {
		area.graphics.clear();
		area.graphics.beginFill(0x000000, 0);
		area.graphics.moveTo(0, 0);
		area.graphics.lineTo(w, 0);
		area.graphics.lineTo(w, h - 50);
		area.graphics.lineTo(w - 130, h - 50);
		area.graphics.lineTo(w - 130, h);
		area.graphics.lineTo(0, h);
		area.graphics.lineTo(0, 0);
	}
		
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				PLAY CHANGE EVENT
	// _____________________________________________________________________________________________________________
		
		private function onPlayChange(event:Event=null):void {
			trace("Casted play state: " + player.status);
			switch(player.status) {
				case "playing": 
					TweenMax.to(objects.playBtn, 0.5, { autoAlpha:0 } );
					TweenMax.to(objects.pauseBtn, 0.5, { autoAlpha:1 } );
					TweenMax.to(objects.bigButton, 0.5, { autoAlpha:0 } );
					checkQuality();
					arrangePlayer();
					
					hidePanels();
				
				break;
				case "paused":
					TweenMax.to(objects.playBtn, 0.5, { autoAlpha:1 } );
					TweenMax.to(objects.bigButton, 0.5, { autoAlpha:1 } );
					TweenMax.to(objects.pauseBtn, 0.5, { autoAlpha:0 } );
					objects.bigButton.addEventListener(MouseEvent.CLICK, bigPlay);
					
					hidePanels();
				break;
				case "stopped":
					player.checkYoutube(player.mps.media) ? youtubeEnabled = true : youtubeEnabled = false;
					resize();
				break;
				case null:
					player.checkYoutube(player.mps.media) ? youtubeEnabled = true : youtubeEnabled = false;
					resize();
				break;
				}
		}
		
		private function bigPlay(e:MouseEvent):void 
		{
			if (player.mps.mediaPlayer.canPlay) {
				player.mps.mediaPlayer.play();
				objects.bigButton.removeEventListener(MouseEvent.CLICK, bigPlay);
			}
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				TIME COMPLETED EVENT
	// _____________________________________________________________________________________________________________
		
		private function onTimeCompleted(event:Event):void {
			trace ("Casted time completed, launching next video...");
			
			if (sp.loopEnabled || i < playlist[n].length -1 ) {
				
				TweenMax.allTo([objects.shader, objects.ring], 0.5, { autoAlpha:1 } );
				
				if (player.checkYoutube(player.element) || playMode == "audio") {
					onReady();
				} else {
					player.addEventListener("Ready", onReady);
				}
			} else {
				
					launch(content[n].cover, "image");
					objects.bigPlayBtn.visible = false;
					objects.replayBtn.visible = true;
					objects.replayBtn.alpha = 1;
					
				}
		}
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				READY EVENT & PLAYLIST
	// _____________________________________________________________________________________________________________
		
		private function onReady(event:Event=null):void {
			trace ("Next video summoned.");
			
			if (i < playlist[n].length -1) {
					i++;
				} else { 
					i = 0;
					if (n < playlist.length - 1 ) { n++; } else { n = 0; }
				}
			
			if (sp.shuffleEnabled) { 
				s = Number(random[n]); 
				launch(playlist[s][i].path, playlist[s][i].type);
				} else {
				launch(playlist[n][i].path, playlist[n][i].type);	
				}
				
			trace ("s: " + s);	
			trace ("n: " + n);	
			trace ("i: " + i);
			//trace ("launching: " + playlist[n][i].path);
			
			player.removeEventListener("Ready", onReady);
			TweenMax.allTo([objects.shader, objects.ring], 0.5, { autoAlpha:0 } )
		}
	
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				STATE, BUFFER & TIME CHANGE
	// _____________________________________________________________________________________________________________
	
		private function onStateChange(event:Event):void {
			trace ("State changed: " + player.state);
			switch (player.state){
				case "uninitialized": youtubeEnabled = false;
				break;
				case "playing":
				player.checkYoutube(player.mps.media) ? youtubeEnabled = true : youtubeEnabled = false;
				TweenMax.to(objects.bigButton, 0.5, { autoAlpha:0 } );
			
					checkQuality();
					// force youtube to switch in hd while playing
					if (hdClicked == false && setup.autoQuality == "false") {
						setup.indexLevel > currentQuality ? player.mps.mediaPlayer.switchDynamicStreamIndex(setup.indexLevel) : trace ("No higher quality found.");
					}
					
					
				break;
				}
		}
		
		private function onBufferChange(event:Event):void {
			trace("Casted buffer state: " + player.buffering);
			//player.buffering ? TweenMax.allTo([objects.shader, objects.ring], 0.5, { autoAlpha:1 } ) : TweenMax.allTo([objects.shader, objects.ring], 0.5, { autoAlpha:0 } );
			
			if (player.buffering) {
				TweenMax.allTo([objects.shader, objects.ring], 0.5, { autoAlpha:1 } );
				TweenMax.to(objects.bigButton, 0.5, { autoAlpha:0 } );
			} else {
				TweenMax.allTo([objects.shader, objects.ring], 0.5, { autoAlpha:0 } );	
				player.mps.mediaPlayer.playing ? TweenMax.to(objects.bigButton, 0.5, { autoAlpha:0 } ) : TweenMax.to(objects.bigButton, 0.5, { autoAlpha:1 } );
			}
		}
	
	
		private function onCurrentTimeChange(event:Event):void
		{
			// update progress bar
			var progressAmount:Number = player.mps.mediaPlayer.currentTime / player.mps.mediaPlayer.duration;
			seekbar.updateProgress(progressAmount, setup.seekbarHeight, setup.seekbarRadius, setup.seekbarProgressColor, setup.seekbarPadding);
			
			var cTime = timeUtil.formatTime(player.mps.mediaPlayer.currentTime);
			var tTime = timeUtil.formatTime(player.mps.mediaPlayer.duration);
			var lTime = timeUtil.formatTime(player.mps.mediaPlayer.duration - player.mps.mediaPlayer.currentTime);
			
			playMode != "vast" && playMode != "advert"? totalTime.timeText.htmlText = tTime : totalTime.timeText.htmlText = lTime;
			
			currentTime.timeText.htmlText = timeUtil.formatTime(player.mps.mediaPlayer.currentTime);
			
		}

	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				PLAY/PAUSE BEHAVIOUR
	// _____________________________________________________________________________________________________________
		
		private function onClick(event:MouseEvent):void
		{
			// toggle pause-play
			player.mps.mediaPlayer.playing ? player.mps.mediaPlayer.pause() : player.mps.mediaPlayer.play();
		}
		
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				VOLUME FUNCTIONS
	// _____________________________________________________________________________________________________________
		
		private function volumeClick(e:MouseEvent):void 
		{
			trace("toggling mute");
			
			if (player.mps.mediaPlayer.volume > 0)
				{
					volumeLevel = player.mps.mediaPlayer.volume;
					
					player.mps.mediaPlayer.volume = 0;
					volumebar.updatelevel(0, setup.volumebarHeight, setup.volumebarRadius, setup.volumebarLevelColor, setup.volumebarPadding);
					drawVolumeStatus(0);
				
					} else {
					
					player.mps.mediaPlayer.volume = volumeLevel;
					volumebar.updatelevel(volumeLevel, setup.volumebarHeight, setup.volumebarRadius, setup.volumebarLevelColor, setup.volumebarPadding);
					drawVolumeStatus(volumeLevel);
				}
			
		}
		
		private function drawVolumeStatus(levelAmount:Number):void
		{
			if (levelAmount < 0) { levelAmount = 0 };
			
			if (levelAmount > 0.7) {
					TweenMax.to(objects.volumeBtn.mid, 0.5, { alpha:1 } );
					TweenMax.to(objects.volumeBtn.low, 0.5, { alpha:1 } );
					} else {
					TweenMax.to(objects.volumeBtn.mid, 0.5, { alpha:0 } );
					TweenMax.to(objects.volumeBtn.low, 0.5, { alpha:1 } );
					}
					
			if (levelAmount < 0.01) {
					TweenMax.to(objects.volumeBtn.mid, 0.5, { alpha:0 } );
					TweenMax.to(objects.volumeBtn.low, 0.5, { alpha:0 } );
					TweenMax.to(objects.volumeBtn.none, 0.5, { alpha:1 } );
					} else {
					TweenMax.to(objects.volumeBtn.none, 0.5, { alpha:0 } );	
					}
			
			volumeTooltip.item.htmlText = String( Math.floor(levelAmount * 100) + "%");
			volumeTooltip.item.x = Math.floor((volumeTooltip.width - volumeTooltip.item.width) * .5);
			
			showVolumeTooltip();
		}
		
		private function showVolumeTooltip():void
		{
			if (volumebar.level != null) {
				volumeTooltip.x = Math.floor(volumebar.x + volumebar.level.width - volumeTooltip.width * .5+2);
				if (volumeTooltip.alpha < 1) { TweenMax.to(volumeTooltip, 0.5, { autoAlpha:1 } ) };
			}
		}
		
		private function updateVolume(w:Number):void {
			
			var levelAmount = player.mps.mediaPlayer.volume;
			var volumeWidth = 40;
			volumebar.resize(volumeWidth, setup.volumebarHeight, setup.volumebarRadius, setup.volumebarTrackColor, setup.volumebarLevelColor, setup.volumebarPadding, levelAmount);
			volumebar.x = Math.floor(seeker.x + seekbar.track.width + 90);
			objects.volumeBtn.x = Math.floor(volumebar.x - 28);
		}
		
		private function volumebarClick(event:MouseEvent):void
		{
			if (volumebar.hitTestPoint(mouseX, mouseY)) {
				// calculate and draw the volume level
				var levelAmount:Number = volumebar.track.mouseX / volumebar.track.width;
				volumebar.updatelevel(levelAmount, setup.volumebarHeight, setup.volumebarRadius, setup.volumebarLevelColor, setup.volumebarPadding);
				
				drawVolumeStatus(levelAmount);
				
				// apply it to the mediaplayer
				player.mps.mediaPlayer.volume = levelAmount;
				trace("Current volume is: " + player.mps.mediaPlayer.volume);
			}
			
		}
		
		private function scrubVolume(e:MouseEvent):void 
		{
			volumebar.addEventListener(MouseEvent.MOUSE_MOVE, volumebarClick, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_UP, resetVolume, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, volumebarClick, false, 0, true);
		}
		
		private function resetVolume(e:MouseEvent):void 
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, volumebarClick);
			stage.removeEventListener(MouseEvent.MOUSE_UP, resetVolume);
			volumebar.removeEventListener(MouseEvent.MOUSE_MOVE, volumebarClick);
			volumebar.removeEventListener(MouseEvent.MOUSE_UP, resetVolume);
		}
		
		private function hideVolumeTooltip(e:MouseEvent):void
		{
			TweenMax.to(volumeTooltip, 0.5, { autoAlpha:0 } );
		}
		
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																				FULLSCREEN FUNCTION
	// _____________________________________________________________________________________________________________
		
		private function toggleFullscreen(e:MouseEvent = null):void {
			//stage.displayState == "normal"? stage.displayState = "fullScreen": stage.displayState = "normal";
			
			if (stage.displayState == "normal") {
				stage.displayState = "fullScreen";
				oldW = w;
				oldH = h;
				oldX = main.x;
				oldY = main.y;
				
				w = stage.stageWidth;
				h = stage.stageHeight;
				main.x = 0;
				main.y = 0;
				
				spectrum.filtersEnabled = false;
				} else {
				stage.displayState = "normal";
				spectrum.filtersEnabled = true;
				
				w = oldW;
				h = oldH;
				main.x = oldX;
				main.y = oldY;
				}
				
			resize();
		}
		
		private function onFullScreenEvent(e:FullScreenEvent):void {
			
			if (stage.displayState == "normal") {
				w = oldW;
				h = oldH;
				
				main.x = oldX;
				main.y = oldY;
				
				resize();
				
				spectrum.filtersEnabled = true;
				}
			
		}
		
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																					RIGHT CLICK CONTEXT MENU
	// _____________________________________________________________________________________________________________	
				
			public function initMenu() {
				var menu:ContextMenu = new ContextMenu();
				menu.hideBuiltInItems();

				var item:ContextMenuItem = new ContextMenuItem(setup.contextMenuLabel);
				item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, link, false, 0, true);
				menu.customItems.push(item);
				this.contextMenu = menu;
			}
		
			public function link(e:ContextMenuEvent):void {
				var url:String = setup.contextMenuLink;
				var request:URLRequest = new URLRequest(url);
				navigateToURL(request, "_blank");
			}
			
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																					CLEARING FUNCTION
	// _____________________________________________________________________________________________________________		
			
		private function clear(e:Event):void 
		{
			trace ("clearing...");
			removeEventListener(Event.REMOVED_FROM_STAGE, clear);
			stage.removeEventListener(Event.RESIZE, resize);
			stage.removeEventListener(FullScreenEvent.FULL_SCREEN, onFullScreenEvent);
			
			player.clear();
			player.removeEventListener("PluginLoaded", onPluginLoaded);
			player.removeEventListener("PluginError", onPluginError);
			pl.removeEventListener("PlaylistReady", plReady);
			
			pl.clear();
			spectrum.clear();
			des.clear();
			sp.clear();
			
			sp.settings.scale.removeEventListener(MouseEvent.CLICK, toggleScale);
			
			for (var i = 0; i < pl.entries.numChildren; i++) {
					var obj = pl.entries.getChildAt(i);
					obj.removeEventListener(MouseEvent.CLICK, plClick);
					obj.removeEventListener(MouseEvent.MOUSE_OVER, plOver);
					obj.removeEventListener(MouseEvent.MOUSE_OUT, plOut);
				}
				
				
			player.mps.removeEventListener(Event.RESIZE, resize);
				
			objects.volumeBtn.removeEventListener(MouseEvent.MOUSE_OUT, hideVolumeTooltip);
			volumebar.removeEventListener(MouseEvent.MOUSE_OUT, hideVolumeTooltip);
			
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER, timerOver);
			timer = null;
			
			main.removeEventListener(MouseEvent.ROLL_OVER, showControls);
			main.removeEventListener(MouseEvent.MOUSE_MOVE, handleControls);
			main.removeEventListener(MouseEvent.CLICK, handleControls);
			
			stage.removeEventListener(Event.MOUSE_LEAVE, mouseLeave);
			
			player.removeEventListener("TimeChange", onCurrentTimeChange);
			player.removeEventListener("BufferChange", onBufferChange);
			player.removeEventListener("PlayChange", onPlayChange);
			player.removeEventListener("LoadChange", onLoadChange);
			player.removeEventListener("StateChange", onStateChange);
			player.removeEventListener("TimeCompleted", onTimeCompleted);
			
			objects.bigButton.removeEventListener(MouseEvent.ROLL_OVER, bigButtonOver);
			objects.bigButton.removeEventListener(MouseEvent.ROLL_OUT, bigButtonOut);
			
			queue.dispose(true);
			
			spectrum.enabled = false;
			
			objects.volumeBtn.removeEventListener(MouseEvent.MOUSE_OUT, hideVolumeTooltip);
			volumebar.removeEventListener(MouseEvent.MOUSE_OUT, hideVolumeTooltip);
			
			icons.removeChild(objects.fsBtn);
			icons.removeChild(objects.hdBtn);
			
			icons.removeChild(objects.playBtn);
			icons.removeChild(objects.pauseBtn);
			
			icons.removeChild(objects.volumeBtn);
			controls.removeChild(volumeTooltip);

			controls.removeChild(currentTime);
			controls.removeChild(totalTime);
			controls.removeChild(icons);
			controls.removeChild(volumebar);
			seeker.removeChild(seekbar);
	
			seeker.removeChild(seekerTooltip);
			gui.removeChild(objects.header);
			controls.removeChild(controlsBg);
			controls.removeChild(seeker);
			
			main.removeChild(objects.mainMask);
			main.removeChild(objects.playerMask);
			main.removeChild(objects.spectrumMask);
			main.removeChild(spectrum);
			spectrum.mask = null;
		
			gui.removeChild(controls);
			
			gui.mask = null;

			gui.removeChild(des);
			gui.removeChild(pl);
			gui.removeChild(sp);
			
			gui.removeChild(objects.bigButton);
			gui.removeChild(objects.ring);
			
			main.removeChild(objects.shader);
			
			main.removeChild(gui);
			removeChild(main);
			
			player = null;
			
			playlist = content = preroll = postroll = null;
			
			trace ("cleared!");
		}
			
	}
		
}